<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/17
 * Time: 11:46
 */
namespace Home\Controller;
header("Content-Type:text/html;charset=utf-8");
use Think\Controller;

class IndexController extends Controller  {

        public function __construct(){
            parent::__construct();
/*             print_r($_SESSION);die;*/
            if (!$_SESSION['c_user']||!$_SESSION['comid']){
                $this->error('还没有登录，正在跳转到登录页',U('Home/Login/login'));
            }
            $res = M('research')->where(['uid'=>$_SESSION['comid']])->find();
            if (!$res && ACTION_NAME!='research' && ACTION_NAME!='loginout' && ACTION_NAME!='research_run') {
                $this->redirect('research');
            }
        }

        public function loginout(){

            unset($_SESSION['c_user']);
            unset($_SESSION['comid']);
            if ($_SESSION['c_user']==''&& $_SESSION['comid'] == ''){
                $this->redirect('login/login');
            }

        }

        public function index(){

            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            //未填写
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            //已完成
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $res = array();
            if ($w_table){
                $info = $w_table[0];
                if ($info['cache'] == ''){
                    $res = json_decode(trim($info['install_info']),true);
                }else{
                    $res = json_decode(trim($info['cache']),true);
                }
            }
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
            //先进类别
            $xj_type = [0=>['name'=>'高性能纤维及复合材料'],1=>['name'=>'新型金属功能材料'],2=>['name'=>'新型能源材料'],3=>['name'=>'绿色建筑材料(建筑工业化部品部件)'],4=>['name'=>'3D打印材料(增材制造)']];
            //智能类别
            $zn_type = [0=>['name'=>'新能源装置装备'],1=>['name'=>'新型商用车'],2=>['name'=>'节能环保装备'],3=>['name'=>'智能终端'],4=>['name'=>'大数据']];
            $city = M('city_list')->select();
            $this->assign('zn_type',$zn_type);
            $this->assign('xj_type',$xj_type);
            $country = M('country_list')->select();
            $goods = M('goods_catgory')->select();
            $this->assign('city',$city);
            $this->assign('country',$country);
            $this->assign('goods',$goods);
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('w_table',$w_table);
            $this->assign('y_table',$y_table);
            $this->assign('e_table',$e_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();

        }
        //待填写详情
        public function table(){

            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $id = I('id');
            $res = array();
            if (true){
                $info = $obj->table('xf_total')->where(array('id'=>$id))->find();
                if ($info['cache'] == ''){
                    $res = json_decode(trim($info['install_info']),true);
                }else{
                    $res = json_decode(trim($info['cache']),true);
                }

            }
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
//先进类别
            $xj_type = [0=>['name'=>'高性能纤维及复合材料'],1=>['name'=>'新型金属功能材料'],2=>['name'=>'新型能源材料'],3=>['name'=>'绿色建筑材料(建筑工业化部品部件)'],4=>['name'=>'3D打印材料(增材制造)']];
            //智能类别
            $zn_type = [0=>['name'=>'新能源装置装备'],1=>['name'=>'新型商用车'],2=>['name'=>'节能环保装备'],3=>['name'=>'智能终端'],4=>['name'=>'大数据']];
            $city = M('city_list')->select();
            $this->assign('zn_type',$zn_type);
            $this->assign('xj_type',$xj_type);
            $country = M('country_list')->select();
            $goods = M('goods_catgory')->select();
            $this->assign('city',$city);
            $this->assign('country',$country);
            $this->assign('goods',$goods);
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('e_table',$e_table);
            $this->assign('w_table',$w_table);
            $this->assign('y_table',$y_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();

        }
        //待审核列表详情
        public function d_table(){
            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $id = I('id');
            $res = array();
            if (true){
                $info = $obj->table('xf_total')->where(array('id'=>$id))->find();
                if ($info['is_write'] == 1){
                    $res = json_decode(trim($info['finnal_info']),true);
                }else{
                    $res = json_decode(trim($info['install_info']),true);
                }
            }
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('w_table',$w_table);
            $this->assign('y_table',$y_table);
            $this->assign('e_table',$e_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();
        }
        //未通过详情
        public function n_table(){
            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $id = I('id');
            $res = array();
            if (true){
                $info = $obj->table('xf_total')->where(array('id'=>$id))->find();
                if ($info['is_write'] == 1){
                    $res = json_decode(trim($info['finnal_info']),true);
                }else{
                    $res = json_decode(trim($info['install_info']),true);
                }
            }
            //先进类别
            $xj_type = [0=>['name'=>'高性能纤维及复合材料'],1=>['name'=>'新型金属功能材料'],2=>['name'=>'新型能源材料'],3=>['name'=>'绿色建筑材料(建筑工业化部品部件)'],4=>['name'=>'3D打印材料(增材制造)']];
            //智能类别
            $zn_type = [0=>['name'=>'新能源装置装备'],1=>['name'=>'新型商用车'],2=>['name'=>'节能环保装备'],3=>['name'=>'智能终端'],4=>['name'=>'大数据']];
            $city = M('city_list')->select();
            $this->assign('zn_type',$zn_type);
            $this->assign('xj_type',$xj_type);
            $country = M('country_list')->select();
            $goods = M('goods_catgory')->select();
            $this->assign('city',$city);
            $this->assign('country',$country);
            $this->assign('goods',$goods);
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('w_table',$w_table);
            $this->assign('y_table',$y_table);
            $this->assign('e_table',$e_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();
        }

        public function w_table(){

            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $id = I('id');
            $res = array();
            if (true){
                $info = $obj->table('xf_total')->where(array('id'=>$id))->find();
                if ($info['is_write'] == 1){
                    $res = json_decode(trim($info['finnal_info']),true);
                }else{
                    $res = json_decode(trim($info['install_info']),true);
                }
            }
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('w_table',$w_table);
            $this->assign('e_table',$e_table);
            $this->assign('y_table',$y_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();

        }
        //已过期详情
        public function e_table(){
            $uid = $_SESSION['comid'];
            $obj = D('B2c');
            $con1 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('egt',time())
            );
            $con2 = array(
                'ComID' => $uid,
                'state' => array('eq',3)
            );
            //待审核
            $con3 = array(
                'ComID' => $uid,
                'state' => array('eq',1)
            );
            //未通过
            $con4 = array(
                'ComID' => $uid,
                'state' => array('eq',2)
            );
            //已过期
            $con5 = array(
                'ComID' => $uid,
                'state' => array('eq',0),
                'endtime' => array('lt',time())
            );
            $id = I('id');
            $res = array();
            if (true){
                $info = $obj->table('xf_total')->where(array('id'=>$id))->find();
                if ($info['is_write'] == 1){
                    $res = json_decode(trim($info['finnal_info']),true);
                }else{
                    $res = json_decode(trim($info['install_info']),true);
                }
            }
            $w_table = $obj->table('xf_total')->where($con1)->order('endtime asc')->select();
            $d_table = $obj->table('xf_total')->where($con3)->order('endtime asc')->select();
            $e_table = $obj->table('xf_total')->where($con5)->order('endtime asc')->select();
            $n_table = $obj->table('xf_total')->where($con4)->order('endtime asc')->select();
            $y_table = $obj->table('xf_total')->where($con2)->select();
            $this->assign('res',$res);
            $this->assign('info',$info);
            $this->assign('w_table',$w_table);
            $this->assign('e_table',$e_table);
            $this->assign('y_table',$y_table);
            $this->assign('d_table',$d_table);
            $this->assign('n_table',$n_table);
            $this->display();

        }

        public function table_download(){

            $id = I('id');
            $obj = D('B2c');
            $data = $obj->table('xf_total')->where(array('id'=>$id))->find();
            //生成导出数据的数组
            if ($data['is_write'] == 1){
                $res = json_decode(trim($data['finnal_info']),true);
            }else{
                $res = json_decode(trim($data['install_info']),true);
            }

            //季度
            $quarter = $data['quarter'];
            $filename = $data['title'].$data['year'].'年'.$data['quarter'].'季度'.rand(11,99);
            $title = $data['title'];
            upload_exl($res,$filename,$quarter,$title);

        }

        public function save_table(){
            if (!ISAJAX){
                $this->error('提交方式不正确',0,0);
            }
            $id = I('tableid');
            $obj = D('B2c');
            $last = I('last');
            $now = I('now');

            $tableinfo = $obj->table('xf_total')->where(array('id'=>$id))->field('install_info')->find();
            $tablearr = json_decode(strval(trim($tableinfo['install_info'])),true);
            $i = 0;
            foreach ($tablearr as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k=='last'){
                        $tablearr[$key][$k] = $last[$i];
                    }elseif ($k == 'now'){
                        $tablearr[$key][$k] = $now[$i];
                    }
                }
                $i++;
            }
            $str = json_encode($tablearr);

            $res = $obj->table('xf_total')->where(array('id'=>$id))->setField('cache',$str);
            if ($res){
                $data['state'] = 1;
                $data['msg'] = '缓存成功';
                $this->ajaxReturn($data);
            }else{
                $data['state'] = 2;
                $data['msg'] = '缓存失败';
                $this->ajaxReturn($data);
            }
        }

        public function table_upload(){

            if (!IS_AJAX){
                $this->error('提交方式不正确',0,0);
            }

            $id = I('tableid');
            if ($_FILES){
                $upload = New \Think\Upload();
                $upload->maxSize = 10485760;// 设置附件上传大小
                $upload->exts = array('xls','xlsx');// 设置附件上传类型
                $upload->rootPath = './Public/uploads/excel/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->saveRule = 'time';
                if (!file_exists($upload->rootPath)){
                    mkdir($upload->rootPath,0777);
                }
                $info = $upload->upload();

                if ($info) {
                    $excel_url = '/uploads/excel/' . $info['excel']['savepath'] . $info['excel']['savename'];//如果上传成功则完成路径拼接
                }else{
                    $this->error('上传文件失败');
                }
                $fileurl =  './Public'.$excel_url;
                $data = add_exl($fileurl);
                $count1 = count($data);
                $i = 0;
                foreach ($data as $k=>$v){
                    $arr[$k]['name']=$v[$i];
                    $arr[$k]['metering']=$v[$i+1];
                    $arr[$k]['code']=$v[$i+2];
                    $arr[$k]['last']=$v[$i+3];
                    $arr[$k]['now']=$v[$i+4];
                }
                $obj = D('B2c');
                $olddata = $obj->table('xf_total')->where(array('id'=>$id))->field('install_info,is_write,endtime')->find();
                $oldarr = json_decode(strval(trim($olddata['install_info'])),true);
                if (($olddata['is_write'] ==0) && (time()>$olddata['endtime'])){
                    $this->error('报表上报时间已截止',0,0);
                }
                $count2 = count($oldarr)+1;
                if ($count1 != $count2){
                    $this->error('请提交原excel表格',0,0);
                }
                if ($olddata['is_write'] != 1){
                    $obj->table('xf_total')->where(array('id'=>$id))->setField('cache','');
                }
                $newinfo_last = array();
                $newinfo_now = array();
                foreach ($arr as $k=>$v){
                    foreach ($v as $key=>$value){
                        if ($key == 'last'){
                            array_push($newinfo_last,$value);
                        }elseif ($key == 'now'){
                            array_push($newinfo_now,$value);
                        }
                    }
                }

                foreach ($oldarr as $key=>$value){
                    foreach ($value as $k=>$v){

                        if ($k == 'last'){
                            $oldarr[$key][$k] = $newinfo_last[$i];
                        }elseif ($k == 'now'){
                            $oldarr[$key][$k] = $newinfo_now[$i];
                        }

                    }
                    $i++;
                }
                /*print_r($oldarr);die;*/
                $newstr = json_encode($oldarr);
                if ($olddata['is_write'] == 1){
                    $data1 = array(
                        'finnal_info'=>$newstr,
                        'add_time'=>time(),
                        'state'=>1,
                        'fz' => I('fz'),
                        'tb' => I('tb'),
                        'lx' => I('lx'),
                    );
                }else{
                    $data1 = array(
                        'finnal_info'=>$newstr,
                        'add_time'=>time(),
                        'is_write'=>1,
                        'state'=>1,
                        'fz' => I('fz'),
                        'tb' => I('tb'),
                        'lx' => I('lx'),
                    );
                }
                $res = $obj->table('xf_total')->where(array('id'=>$id))->save($data1);
                if ($res){
                    $this->success('修改成功',U('index'),0);
                }
            }

        }

        public function online_run(){

            if (!IS_AJAX){
                $this->error('提交方式不正确',0,0);
            }
            $id= I('tableid');
            $obj = D('B2c');
            $olddata = $obj->table('xf_total')->where(array('id'=>$id))->find();
            $oldarr = json_decode(strval(trim($olddata['install_info'])),true);
            if (($olddata['is_write']==0) && (time()>$olddata['endtime'])){
                $this->error('报表上报时间已截止',0,0);
            }
            if ($olddata['is_write'] != 1){
                $obj->table('xf_total')->where(array('id'=>$id))->setField('cache','');
            }
            $newlast = I('last');
            $newnow = I('now');
            //foreach 定义自增变量
            $i = 0;
            //对相应的数据进行更新
            foreach ($oldarr as $key=>$value){
                foreach ($value as $k=>$v){
                    if ($k == 'last'){
                        $oldarr[$key][$k] = $newlast[$i];
                    }elseif ($k == 'now'){
                        $oldarr[$key][$k] = $newnow[$i];
                    }
                }
                $i++;
            }
            $newstr = json_encode($oldarr);

            $res = $obj->table('xf_total')->where(array('id'=>$id))->setField(array('finnal_info'=>$newstr,'fz'=>I('fz'),'lx'=>I('lx'),'tb'=>I('tb'),'is_write'=>1,'add_time'=>time(),'state'=>1));
            if ($res){
                $this->success('修改成功',U('index'),0);
            }

        }

        public function research(){


            $this->display();

        }

        public function research_run(){

            if (!IS_AJAX) {
                $this->error('提交方式不正确');
            }
            $uid = $_SESSION['comid'];
            $ishave = M('research')->where(['uid'=>$uid])->find();
            if ($ishave) {
                $this->error('你已上报！');
            }
            
            $_POST['uid'] = $uid;
            $_POST['addtime'] = time();
            $res = M('research')->add($_POST);
            if ($res) {
                $this->success('上报成功!',U('index/index'),0);
            }

        }

}